package metier;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import webEquipeRechercheGwt.client.entityMirror.PaperMirror;
import webEquipeRechercheGwt.client.entityMirror.PersonMirror;
import webEquipeRechercheGwt.client.entityMirror.SeminarMirror;
import webEquipeRechercheGwt.client.entityMirror.SoftwareMirror;
import webEquipeRechercheGwt.client.entityMirror.TeamMirror;

@Entity
@Table(name="PERSON")
@NamedQueries(
		@NamedQuery(name="getPersons",
				query="select p from Person p " +
				"order by p.name, p.firstname")
			)
public class Person implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5564806565723561610L;

	private int id;
	private String name;
	private String firstname;
	private String phone;
	private String mail;
	private String affiliation;
	private Collection<Seminar> seminars;
	private Collection<Software> softwares;
	private Collection<Paper> papers;
	private Collection<Team> team;
	private PersonMirror loaded;
	
	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
	public void setAffiliation(String affiliation) {
		this.affiliation = affiliation;
	}
	public String getAffiliation() {
		return affiliation;
	}
	public void setSeminars(Collection<Seminar> seminars) {
		this.seminars = seminars;
	}
	@ManyToMany
	public Collection<Seminar> getSeminars() {
		return seminars;
	}
	public void addSeminar(Seminar s) {
		seminars.add(s);
	}
	
	@ManyToMany
	public Collection<Software> getSoftwares() {
		return softwares;
	}
	public void setSoftwares(Collection<Software> softwares) {
		this.softwares = softwares;
	}
	public void addSoftware(Software s){
		softwares.add(s);
	}
	
	@ManyToMany
	public Collection<Paper> getPapers() {
		return papers;
	}
	public void setPapers(Collection<Paper> papers) {
		this.papers = papers;
	}
	public void addPaper(Paper p){
		papers.add(p);
	}
	public void fullLoad(){
		Iterator<Seminar> it = seminars.iterator();
		while(it.hasNext()){
			Seminar s = it.next();
			s.toString();
		}
		Iterator<Software> it2 = softwares.iterator();
		while(it.hasNext()){
			Software s = it2.next();
			s.toString();
		}
		Iterator<Paper> it3 = papers.iterator();
		while(it.hasNext()){
			Paper s = it3.next();
			s.toString();
		}
	}
	@ManyToMany(mappedBy="members")
	public Collection<Team> getTeam() {
		return team;
	}
	public void setTeam(Collection<Team> team) {
		this.team = team;
	}
	public PersonMirror getMirror() {
		if(loaded==null){
			loaded = new PersonMirror();
			loaded.setId(id);
			loaded.setAffiliation(affiliation);
			loaded.setFirstname(firstname);
			loaded.setMail(mail);
			loaded.setName(name);
			loaded.setPhone(phone);
			Collection<PaperMirror> paperMirrors = new ArrayList<PaperMirror>();
			for(Paper p : papers){
				paperMirrors.add(p.getMirror());
			}
			loaded.setPapers(paperMirrors);

			Collection<SoftwareMirror> softwareMirrors = new ArrayList<SoftwareMirror>();
			for(Software s : softwares){
				softwareMirrors.add(s.getMirror());
			}
			loaded.setPapers(paperMirrors);

			Collection<TeamMirror> teamMirrors = new ArrayList<TeamMirror>();
			for(Team t : team){
				teamMirrors.add(t.getMirror());
			}
			loaded.setTeam(teamMirrors);

			Collection<SeminarMirror> seminarMirrors = new ArrayList<SeminarMirror>();
			for(Seminar s : seminars){
				seminarMirrors.add(s.getMirror());
			}
			loaded.setSeminars(seminarMirrors);
		}
		return loaded;
	}
	
	
}
